//
//  ICMRequestManager.m
//  ICrEAM
//
//  Created by rainimute on 16/11/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMRequestManager.h"
#import "ICMRequest.h"
#import "ICMGetRequest.h"
#import "MBProgressHUD.h"
#import "EXTScope.h"
#import "UploadImageApi.h"
#import "ICMUploadMusicApi.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
@interface ICMRequestManager ()


@end

@implementation ICMRequestManager

- (void)startPostRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameters inHudView:(UIView *)hudView andResultBlock:(callBack)callBack{
    
    NSString *trueUrl = @"";
    if(url)
    {
        if([url containsString:@"http"])
        {
            trueUrl = url;
        }
        else
        {
            trueUrl = [NSString stringWithFormat:@"http://%@%@",[[ICreamCache sharedInstance] stringForKey:kIPPath],url];
        }
    }
    
    ICMRequest *request = [[ICMRequest alloc]initWithURL:trueUrl andParameters:parameters];
    __block MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:hudView];
    [hudView addSubview:hud];
    [hud show:YES];
    [request startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        //王兵代码 cookies过期
        ResponseBase *res=[ResponseBase mj_objectWithKeyValues:request.responseJSONObject];
        
        if (res.CODE==-1) {
            NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
            for (int i = 0; i < [cookies count]; i++) {
                NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
                
            }
            LoginViewController *login=[[LoginViewController alloc]init];
            [AppDelegate currentAppDelegate].window.rootViewController= login;
            return ;
        }
        if(res.CODE != 200)
        {
            [hud hide:YES];
            [hud removeFromSuperview];
            hud = nil;
            //显示后台错误信息
            [hudView showString:res.MSG forSeconds:1.5];
        }
        else
        {
            NSDictionary *responseObject = request.responseJSONObject;
            callBack(responseObject,nil);
            [hud hide:YES];
            [hud removeFromSuperview];
            hud = nil;
        }
    } failure:^(__kindof YTKBaseRequest *request) {
        callBack(nil,@"fail");
        [hud hide:YES];
        [hud removeFromSuperview];
        hud = nil;
    }];
}

- (void)startGetRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameters inHudView:(UIView *)hudView andResultBlock:(callBack)callBack{
    
    ICMGetRequest *request = [[ICMGetRequest alloc]initWithURL:url andParameters:parameters];
    
    __block MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:hudView];
    [hudView addSubview:hud];
    [hud show:YES];
    
    [request startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        //王兵代码 cookies过期
        ResponseBase *res=[ResponseBase mj_objectWithKeyValues:request.responseJSONObject];
        
        if (res.CODE==-1) {
            NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
            for (int i = 0; i < [cookies count]; i++) {
                NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
                
            }
            LoginViewController *login=[[LoginViewController alloc]init];
            [AppDelegate currentAppDelegate].window.rootViewController= login;
            return ;
        }
        if(res.CODE != 200)
        {
            [hud hide:YES];
            [hud removeFromSuperview];
            hud = nil;
            //显示后台错误信息
            [hudView showString:res.MSG forSeconds:1.5];
            return ;
        }
        else
        {
            NSDictionary *responseObject = request.responseJSONObject;
            callBack(responseObject,nil);
            
            [hud hide:YES];
            [hud removeFromSuperview];
            hud = nil;
        }
        
    } failure:^(__kindof YTKBaseRequest *request) {
        callBack(nil,@"fail");
        [hud hide:YES];
        [hud removeFromSuperview];
        hud = nil;
    }];

}

- (void)startPostImageWithURL:(NSString *)url parameters:(NSDictionary *)parameters images:(NSDictionary *)images inHudView:(UIView *)hudView andResultBlock:(callBack)callBack{
    UploadImageApi *imageApi = [[UploadImageApi alloc]initWithImage:images];
    __block MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:hudView];
    [hudView addSubview:hud];
    [hud show:YES];
    [imageApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        NSDictionary *responseObject = request.responseJSONObject;
        callBack(responseObject,nil);
        [hud hide:YES];
        [hud removeFromSuperview];
        hud = nil;
        
    } failure:^(__kindof YTKBaseRequest *request) {
        callBack(nil,@"fail");
        [hud hide:YES];
        [hud removeFromSuperview];
        hud = nil;
    }];
}

- (void)startPostMusicWithURL:(NSString *)url parameters:(NSDictionary *)parameters musicUrl:(NSURL *)musicUrl inHudView:(UIView *)hudView andResultBlock:(callBack)callBack{
    ICMUploadMusicApi *musicApi = [[ICMUploadMusicApi alloc]initWithMusicUrl:musicUrl];
    __block MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:hudView];
    [hudView addSubview:hud];
    [hud show:YES];
    [musicApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        NSDictionary *responseObject = request.responseJSONObject;
        callBack(responseObject,nil);
        [hud hide:YES];
        [hud removeFromSuperview];
        hud = nil;
        
    } failure:^(__kindof YTKBaseRequest *request) {
        callBack(nil,@"fail");
        [hud hide:YES];
        [hud removeFromSuperview];
        hud = nil;
    }];
}



@end
